<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>

<script>
    /* 声明格式：let reg = new RegExp("pattern","[flags(i忽略大小写/g全文匹配/m多行匹配)]") */
    let formatReg1 = new RegExp('cat');
    let formatReg2 = new RegExp('cat', 'gi');

    let str = 'User13UseraaaUser345';

    /* test(str)：根据正则判断字符串是否匹配 */
    console.log(new RegExp('User\\d').test(str));//true
    console.log(new RegExp('User\\d', 'g').test(str));//true
    console.log(new RegExp('user\\d', 'gi').test(str));//true

    /* exec()：根据正则返回匹配子字符串，flag存在g则全文匹配则第一次exec返回第一个匹配的子字符串，第二次exec返回第二个匹配的子字符串 */
    let execReg = new RegExp('User\\d+', 'g');
    console.log(execReg.exec(str));//['User13']
    console.log(execReg.exec(str));//['User345']
</script>
</html>